package annotion.example.hss_fst.baselib.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
//import com.hss.common.utils.log.LogUtils;

/**
 * <b>Project:</b> BaseLib<br>
 * <b>Create Date:</b> 2016/4/29<br>
 * <b>Author:</b> huang saisai <br>
 * <b>Description:</b> <br>
 */
public class GlideFragments extends ListFragment {
    ImageAdapter adapter;

    String[] imgArr = {"http://img.ugirls.com/uploads/cooperate/baidu/20160408jzx3.jpg",
                       "http://e.hiphotos.baidu.com/image/pic/item/e7cd7b899e510fb34395d1c3de33c895d0430cd1.jpg",
                       "http://pic.yesky.com/uploadImages/2015/131/32/WH0O33V7Y7HA.jpg",
                       "http://img.taopic.com/uploads/allimg/120924/219049-12092412401694.jpg"};


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (adapter == null){
            adapter = new ImageAdapter(getActivity());
        }
        this.setListAdapter(adapter);
    }



    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        if (adapter == null){
//            adapter = new ImageAdapter(getActivity());
//        }
//        setListAdapter(adapter);
//        LogUtils.e("onViewCreated");

    }

    class ImageAdapter extends BaseAdapter{

        Context mContext;
        LayoutInflater mLayoutInflater;
        public ImageAdapter(Context mContext) {
            this.mContext = mContext;
            mLayoutInflater = LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() {
            return imgArr == null ? 0 : imgArr.length;
        }

        @Override
        public Object getItem(int position) {
            return imgArr[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = (ImageView) convertView;
            if ( null == imageView){
                imageView = new ImageView(mContext);
            }
            Glide.with(GlideFragments.this).
            load(imgArr[position]).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);

            return imageView;
        }
    }

}
